package fr.mairie.parc.dao.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import fr.mairie.parc.dao.MaterielDao;
import fr.mairie.parc.dao.mapper.MaterielRowMapper;
import fr.mairie.parc.domain.Materiel;

public class MaterielDaoImpl implements MaterielDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public JdbcTemplate getJdbcTempalete() {
		return jdbcTemplate;
	}

	@Autowired
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Materiel> selectAll() {
		return jdbcTemplate.query("SELECT idMateriel, numParc ,typeMateriel, marque, modele,stock, prix" +
				"" + " FROM materiel",
						new MaterielRowMapper());
	}

	@Override
	public void addMateriel(Materiel materiel) {
		jdbcTemplate.update("INSERT INTO materiel (typeMateriel, marque, modele,stock, prix) values (?,?,?,?,?)",
				new Object [] {
							   materiel.getType(),
							   materiel.getMarque(),
							   materiel.getModele(),
							   materiel.getStock(),
							   materiel.getPrix()
							   });
	}

	@Override
	public Materiel getMaterielById(int id) {
		return (Materiel) jdbcTemplate.queryForObject("SELECT * FROM materiel WHERE idMateriel=?",
				new Object [] {id},
				new MaterielRowMapper());
	}

	@Override
	public void updateMateriel(Materiel materiel) {
		jdbcTemplate.update("UPDATE materiel SET  etat=? , numParc= ? WHERE idMateriel= ? ",
                new Object [] {
							   materiel.getEtat(),
							   materiel.getNumParc(),
							   materiel.getIdMateriel()});
	}
		
	

}
